﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLT_BLL;
using QLT_Public;
using QLThuoc_report;

namespace GiaoDien
{
    public partial class uc_HSNhanVien : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_HSNhanVien()
        {
            InitializeComponent();
        }

        private void bii_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_NhanVien1 f = new frm_NhanVien1();
            f.Flag = 0;
            f.MyGetData = new frm_NhanVien1.GetInt(GetLoad);
            f.ShowDialog();
        }
        public void GetLoad(int kt)
        {
            txt_Load.Text = kt.ToString();
        }

        private void bii_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_NhanVien1 f = new frm_NhanVien1();
            f.Flag = 1;
            f.MyGetData = new frm_NhanVien1.GetInt(GetLoad);
            f.MaNV = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhân Viên"));
            f.HoTen = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Họ Tên").ToString();
            f.NgaySinh =Convert.ToDateTime( gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ngày Sinh"));
            f.GioiTinh = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Giới Tính").ToString();
            f.CMT = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Chứng Minh Thư"));
            f.DiaChi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Địa Chỉ").ToString();
            f.DienThoai = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Điện Thoại").ToString();
            f.Email = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Email").ToString();
            f.MaNQ = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Nhóm Quyền").ToString();
            f.TenDangNhap = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đăng Nhập").ToString();
            f.MatKhau = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mật Khẩu").ToString();
            f.ShowDialog();
        }
        NhanVien_BLL nhanVien_bll = new NhanVien_BLL();
        private void uc_HSNhanVien_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = nhanVien_bll.LayDuLieu();
            txt_Load.Hide();
        }
        NhatKy_Bll nhatKy_bll = new NhatKy_Bll();
        private void bii_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {

                NhanVien_Public t = new NhanVien_Public();
                t.MaNV = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhân Viên"));
                t.HoTen = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Họ Tên").ToString();
                if (XtraMessageBox.Show("Bạn có muốn xóa nhân viên: '" + t.HoTen + "' có mã là: '" + t.MaNV + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    NhatKy_Public nk = new NhatKy_Public();
                    string d = DateTime.Now.ToString();
                    Class_TrungGian tg = new Class_TrungGian();
                    nk.TaiKhoan = tg.TaiKhoan;
                    nk.NgayGio = d;
                    nk.DoiTuong = tg.DoiTuong;
                    nk.HanhDong = "Xóa";
                    nk.MoTa = "Xóa thông tin Nhân viên: '" + t.HoTen + "'";
                    nhatKy_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                    nhanVien_bll.delete_NhanVien(t);
                    uc_HSNhanVien_Load(sender, e);
                }
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Không thể xóa do có bản ghi phụ thuộc!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
            }
        }

        private void bbi_Xem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_NhanVien1 f = new frm_NhanVien1();
            f.Flag = 2;
            f.MyGetData = new frm_NhanVien1.GetInt(GetLoad);
            f.MaNV = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhân Viên"));
            f.HoTen = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Họ Tên").ToString();
            f.NgaySinh = Convert.ToDateTime(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ngày Sinh"));
            f.GioiTinh = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Giới Tính").ToString();
            f.CMT = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Chứng Minh Thư"));
            f.DiaChi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Địa Chỉ").ToString();
            f.DienThoai = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Điện Thoại").ToString();
            f.Email = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Email").ToString();
            f.MaNQ = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Nhóm Quyền").ToString();
            f.TenDangNhap = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đăng Nhập").ToString();
            f.MatKhau = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mật Khẩu").ToString();
            f.ShowDialog();
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            frm_NhanVien1 f = new frm_NhanVien1();
            f.Flag = 2;
            f.MyGetData = new frm_NhanVien1.GetInt(GetLoad);
            f.MaNV = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhân Viên"));
            f.HoTen = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Họ Tên").ToString();
            f.NgaySinh =Convert.ToDateTime( gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ngày Sinh"));
            f.GioiTinh = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Giới Tính").ToString();
            f.CMT = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Chứng Minh Thư"));
            f.DiaChi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Địa Chỉ").ToString();
            f.DienThoai = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Điện Thoại").ToString();
            f.Email = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Email").ToString();
            f.MaNQ = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Nhóm Quyền").ToString();
            f.TenDangNhap = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đăng Nhập").ToString();
            f.MatKhau = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mật Khẩu").ToString();
            f.ShowDialog();
        }

        private void txt_Load_TextChanged(object sender, EventArgs e)
        {
            if (txt_Load.Text == "1")
            {
                uc_HSNhanVien_Load(sender, e);
                txt_Load.Text = "0";
            }
        }

        private void bbi_In_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_NhanVien f = new frm_NhanVien();
            f.ShowDialog();
        }
    }
}
